<?php
/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */

class AnnouncementController extends Zend_Controller_Action
{
    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }

    /**
     * This will validate our session and the requestion action;
     */
    public function preDispatch()
    {
        // Check our identity
        if(Zend_Auth::getInstance()->hasIdentity())
        {
            // Check the requestion action
            switch ( $this->getRequest()->getActionName())
            {
                case 'index':
                case 'add':
                case 'edit':
                case 'delete':
                    // -- These actions are allowed, nothing to do -- //
                    break;
                default:
                    // -- Invalid action requested, refer to homepage -- //
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        }
        else
        {
            // -- No session, refer to loginpage -- //
            $this->_helper->redirector('login', 'user');
        }
    }

    /**
     * The main action.
     */
    public function indexAction()
    {
        $this->_helper->redirector('index', 'senior');
    }
    
       
    /**
     * Add a course
     */
    public function addAction()
    {        
        $view = $this->_getView('Add announcement');
        $form = $this->_getAddAnnouncementForm();
        $course_id  = (string)$this->getRequest()->getParam('id');
        
        if($this->getRequest()->isPost()) 
	{                       
            $formData  = $this->_request->getPost();
                        
            if($form->isValid($formData)) 
            {                
               return $this->_saveForm($form);
            }
        }
        else
        {
            $form->populate(array(
                'course_id'     => $course_id, 
            ));
        }
        // Push to form
        $view->form = $form;       
    }
    
    /**
     * Edit a course
     */
    public function editAction()
    {
        // Set up basics
        $view = $this->_getView('Edit Announcement');
        $form = $this->_getEditAnnouncementForm();
        $id  = (string)$this->getRequest()->getParam('id');
        // Are we submitting the form or not?
        if ($this->getRequest()->isPost()) 
	{
            // Get the form data an validate it
            $formData   = $this->_request->getPost();
            $id         = $formData['announcement_id'];
            if($form->isValid($formData))
            {             
                return $this->_saveForm($form);
            }
        }
        else
        {
            $announcement = $this->_getByAnnouncementId($id);         

            if($announcement != null)
            {
                $form->populate(array(
                    'announcement_id'               => $announcement->Id,    
                    'announcement_title'            => $announcement->Title,
                    'announcement_text'             => $announcement->Text,
                    'announcement_creationdate'     => $announcement->CreationDate,  
                    'announcement_active'           => $announcement->Active,
                    'course_id'                     => $announcement->CourseId,
                ));
            }
        }
        // view
        $view->form = $form;
    }
    /**
     * This will let you delete an announcement
     */
    public function deleteAction()
    {
        // Get the ID and delete this announcement
        $id = (string)$this->getRequest()->getParam('id');
        $mapper = new Application_Model_AnnouncementMapper();
        $mapper->delete($id);
        $this->_helper->redirector('index', 'senior');
    }

    
    /**
     * Save
     */
    private function _saveForm($form)
    {
        // This will only be visible if something goes wrong
        $mapper = new Application_Model_AnnouncementMapper();
        $view = $this->_getView('Error - Edit Announcement');

        // Prepare to save
        $values = $form->getValues();
        $announcement = new Application_Model_Announcement();
        $announcement->Id       = $values['announcement_id'];
        $announcement->Title    = $values['announcement_title'];
        $announcement->Text     = $values['announcement_text'];
        $announcement->Active   = $values['announcement_active'];
        $announcement->CourseId = $values['course_id'];

        // Save                
        $mapper->save($announcement);

        // Redirect
        return $this->_helper->redirector('index', 'senior'); 
    }

    /**
     * Fetch an announcement by its ID
     *
     * @param integer $id
     * @return Application_Model_Announcement
     */
    private function _getByAnnouncementId($id)
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        return $announcementMapper->fetchById($id);
    }

    /**
     * Get the edit form
     *
     * @return Application_Form_AddAnnouncement
     */
    private function _getEditAnnouncementForm()
    {   
        return new Application_Form_EditAnnouncement(array(
            'action' => $this->view->baseUrl() . '/announcement/edit/',
            'method' => 'post',
        ));
    }
    
    /**
     * Get the add form
     *
     * @return Application_Form_EditAnnouncement
     */
    private function _getAddAnnouncementForm()
    {   
        return new Application_Form_AddAnnouncement(array(
            'action' => $this->view->baseUrl() . '/announcement/add/',
            'method' => 'post',
        ));
    }

    /**
     * This returns the view for this controller.
     * 
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'PREPEND');

        return $view;
    }
}

